package ObjectFile;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;

/**
 *
 * @author yogesh
 */
public class SourceListing {
    public static File link_sourcelisting_file = new File("");
    String[][] code_sourcelisting;
    String[] linenum_codefile_from_interfile; int line_num_column=5;
    String[][] sourcelisting;
        
    /**
     * This function initializes the code_sourcelisting[][] array.
     * <p>
     * Modified Log:<br>
     * None.<br>
     * <p>
     * Error Condition tested:<br>
     * None.<br>
     * <p>
     * Creation Date : May 13,2008
     * <p>
     * <b>Original Author:</b> Aashish Kumar.
     * @param text_record A 2D string array.
     */
    private void ini_code_sourcelisting(String[][] text_record){
        code_sourcelisting = new String[IceAssembly.Table.intermediate_row][3];
        for(int index=0; index<IceAssembly.Table.intermediate_row; index++){
            code_sourcelisting[index][0] = text_record[index][0];
            code_sourcelisting[index][1] = text_record[index][2];
            code_sourcelisting[index][2] = text_record[index][3];
        }
    }
    
    /**
     * This function initializes the linenum_codefile_from_interfile[] array.
     * <p>
     * Modified Log:<br>
     * None.<br>
     * <p>
     * Error Condition tested:<br>
     * None.<br>
     * <p>
     * Creation Date : May 13,2008
     * <p>
     * <b>Original Author:</b> Aashish Kumar   
     */
    private void ini_line_num(){
        String[][] inter_file_sp1 = IceAssembly.Table.intermediate_file;
        linenum_codefile_from_interfile = new String[IceAssembly.Table.intermediate_row];
        for(int index=0; index<IceAssembly.Table.intermediate_row; index++){
            linenum_codefile_from_interfile[index] = inter_file_sp1[index][line_num_column];
        }
    }
    
    /**
     * This function writes the contents of sourcelisting[][] array to the drive.
     * <p>
     * Modified Log:<br>
     * None.<br>
     * <p>
     * Error Condition tested:<br>
     * None.<br>
     * <p>
     * Creation Date : May 14,2008
     * <p>
     * <b>Original Author:</b> Aashish Kumar   
     */
    private void writeToDrive(){        
        try{            
            BufferedWriter bw = new BufferedWriter(new FileWriter(link_sourcelisting_file));
            bw.write("LC\tHex\tAREC\t|\tLine\tSource\n");
            for(int i=0; i<sourcelisting.length; i++){
                bw.write(sourcelisting[i][0]+"\t"+sourcelisting[i][1]+"\t"+sourcelisting[i][2]+"\t|\t"+sourcelisting[i][3]+"\t"+sourcelisting[i][4]);
                bw.write("\n");
            }
            bw.close();
        }catch(Exception e){
            System.out.println("Writing to sourcelisting.txt file failed");
        }
    }
    
    /**
     * This function initializes the sourcelisting[][] array.
     * <p>
     * Modified Log:<br>
     * None.<br>
     * <p>
     * Error Condition tested:<br>
     * None.<br>
     * <p>
     * Creation Date : May 13,2008
     * <p>
     * <b>Original Author:</b> Aashish Kumar
     * @param text_record A 2D string array.
     */
    public void ini_sourcelisting_array(String[][] text_record){
        ini_line_num();
        ini_code_sourcelisting(text_record);
        sourcelisting = new String[code_sourcelisting.length][5];
        for(int index=0; index<sourcelisting.length; index++){
            sourcelisting[index][0] = code_sourcelisting[index][0];
            sourcelisting[index][1] = code_sourcelisting[index][1];
            sourcelisting[index][2] = code_sourcelisting[index][2];
            sourcelisting[index][3] = String.valueOf(Integer.parseInt(linenum_codefile_from_interfile[index])+1);
            sourcelisting[index][4] = IceAssembly.Control.code_string[Integer.parseInt(linenum_codefile_from_interfile[index])];
        }
        writeToDrive();
    }
}
